package com.alex.wechat.mp.fans.service.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.core.util.MyClassLoader;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.cgform.enhance.CgformEnhanceJavaInter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alex.wechat.mp.fans.entity.WxFansEntity;
import com.alex.wechat.mp.fans.service.WxFansServiceI;
import com.alex.wechat.mp.util.EncodeUtil;
import com.gomore.jeecg.common.Assert;

@Service("wxFansService")
@Transactional
public class WxFansServiceImpl extends CommonServiceImpl implements WxFansServiceI {

  // @Autowired
  // private MemberServiceI mbrService;

  public void delete(WxFansEntity entity) throws Exception {
    super.delete(entity);
    // 执行删除操作增强业务
    this.doDelBus(entity);
  }

  @Override
  public Serializable save(WxFansEntity entity) throws Exception {
    filterNickName(entity);
    Serializable t = super.save(entity);
    // 执行新增操作增强业务
    this.doAddBus(entity);
    return t;
  }

  @Override
  public void saveOrUpdate(WxFansEntity entity) throws Exception {
    filterNickName(entity);
    super.saveOrUpdate(entity);
    // 执行更新操作增强业务
    this.doUpdateBus(entity);
  }

  /**
   * 新增操作增强业务
   * 
   * @param t
   * @return
   */
  private void doAddBus(WxFansEntity t) throws Exception {
    // ensureSaveMember(t);
  }

  /**
   * 更新操作增强业务
   * 
   * @param t
   * @return
   */
  private void doUpdateBus(WxFansEntity t) throws Exception {
    // ensureSaveMember(t);
  }

  // private void ensureSaveMember(WxFansEntity t) {
  // MemberEntity member = mbrService.findUniqueByProperty(MemberEntity.class,
  // "wxid",
  // t.getOpenid());
  // if (member != null) {
  // member.setName(t.getNickname());
  // member.setImgurl(t.getHeadimgurl());
  // mbrService.saveOrUpdate(member);
  // } else {
  // MemberEntity mbr = new MemberEntity();
  // mbr = new MemberEntity();
  // mbr.setName(t.getNickname());
  // mbr.setWxid(t.getOpenid());
  // mbr.setImgurl(t.getHeadimgurl());
  // mbr.setGrade(MemberGrade.GRADE_1);
  // mbr.setStatus(MemberState.NORMAL);
  // mbrService.save(mbr);
  // }
  // }

  /**
   * 删除操作增强业务
   * 
   * @param id
   * @return
   */
  private void doDelBus(WxFansEntity t) throws Exception {
  }

  private Map<String, Object> populationMap(WxFansEntity t) {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("id", t.getId());
    map.put("create_name", t.getCreateName());
    map.put("create_by", t.getCreateBy());
    map.put("create_date", t.getCreateDate());
    map.put("account_id", t.getAccountId());
    map.put("openid", t.getOpenid());
    map.put("nickname", t.getNickname());
    map.put("sex", t.getSex());
    map.put("city", t.getCity());
    map.put("country", t.getCountry());
    map.put("province", t.getProvince());
    map.put("language", t.getLanguage());
    map.put("headimgurl", t.getHeadimgurl());
    map.put("subscribe_time", t.getSubscribeTime());
    map.put("unionid", t.getUnionid());
    map.put("remark", t.getRemark());
    map.put("groupid", t.getGroupid());
    map.put("subscribe", t.getSubscribe());
    return map;
  }

  /**
   * 替换sql中的变量
   * 
   * @param sql
   * @param t
   * @return
   */
  public String replaceVal(String sql, WxFansEntity t) {
    sql = sql.replace("#{id}", String.valueOf(t.getId()));
    sql = sql.replace("#{create_name}", String.valueOf(t.getCreateName()));
    sql = sql.replace("#{create_by}", String.valueOf(t.getCreateBy()));
    sql = sql.replace("#{create_date}", String.valueOf(t.getCreateDate()));
    sql = sql.replace("#{account_id}", String.valueOf(t.getAccountId()));
    sql = sql.replace("#{openid}", String.valueOf(t.getOpenid()));
    sql = sql.replace("#{nickname}", String.valueOf(t.getNickname()));
    sql = sql.replace("#{sex}", String.valueOf(t.getSex()));
    sql = sql.replace("#{city}", String.valueOf(t.getCity()));
    sql = sql.replace("#{country}", String.valueOf(t.getCountry()));
    sql = sql.replace("#{province}", String.valueOf(t.getProvince()));
    sql = sql.replace("#{language}", String.valueOf(t.getLanguage()));
    sql = sql.replace("#{headimgurl}", String.valueOf(t.getHeadimgurl()));
    sql = sql.replace("#{subscribe_time}", String.valueOf(t.getSubscribeTime()));
    sql = sql.replace("#{unionid}", String.valueOf(t.getUnionid()));
    sql = sql.replace("#{remark}", String.valueOf(t.getRemark()));
    sql = sql.replace("#{groupid}", String.valueOf(t.getGroupid()));
    sql = sql.replace("#{subscribe}", String.valueOf(t.getSubscribe()));
    sql = sql.replace("#{UUID}", UUID.randomUUID().toString());
    return sql;
  }

  /**
   * 执行JAVA增强
   */
  private void executeJavaExtend(String cgJavaType, String cgJavaValue, Map<String, Object> data)
      throws Exception {
    if (StringUtil.isNotEmpty(cgJavaValue)) {
      Object obj = null;
      try {
        if ("class".equals(cgJavaType)) {
          // 因新增时已经校验了实例化是否可以成功，所以这块就不需要再做一次判断
          obj = MyClassLoader.getClassByScn(cgJavaValue).newInstance();
        } else if ("spring".equals(cgJavaType)) {
          obj = ApplicationContextUtil.getContext().getBean(cgJavaValue);
        }
        if (obj instanceof CgformEnhanceJavaInter) {
          CgformEnhanceJavaInter javaInter = (CgformEnhanceJavaInter) obj;
          javaInter.execute(data);
        }
      } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("执行JAVA增强出现异常！");
      }
    }
  }

  @Override
  @Transactional(readOnly = true)
  public WxFansEntity getByOpenId(String openId) {
    Assert.assertArgumentNotEmpty(openId, "openId");
    return findUniqueByProperty(WxFansEntity.class, "openid", openId);
  }

  private void filterNickName(WxFansEntity fans) {
    String nickname = fans.getNickname();
    try {
      nickname = EncodeUtil.filterOffUtf8mb4(nickname);
    } catch (Exception e) {
      nickname = fans.getOpenid();
    }
    fans.setNickname(nickname);
  }

}